package com.slz3.business.module.wmsManagement.wmsPurchaseOrderSelf.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.goodsManagement.gooBaseGoodsAttribute.domain.GooBaseGoodsAttribute;
import com.slz3.business.module.wmsManagement.wmsPurchaseOrderLine.domain.WmsPurchaseOrderLine;
import com.slz3.business.module.wmsManagement.wmsPurchaseOrderSelf.domain.WmsPurchaseOrderSelf;
import com.slz3.business.module.wmsManagement.wmsPurchaseOrderSelf.domain.WmsPurchaseOrderSelfFactory;
import com.slz3.business.module.wmsManagement.wmsPurchaseOrderSelf.steward.WmsPurchaseOrderSelfSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class WmsPurchaseOrderSelfServiceImpl implements WmsPurchaseOrderSelfService {
    @Override
    public WmsPurchaseOrderSelf save(WmsPurchaseOrderSelf wmsPurchaseOrderSelf) {
        return Repositorys.wmsPurchaseOrderSelf.save(WmsPurchaseOrderSelfFactory.initBaseFields(wmsPurchaseOrderSelf));
    }

    @Override
    public Page<WmsPurchaseOrderSelf> page(MultiValueMap<String, String> params) {
        Page<WmsPurchaseOrderSelf> wmsPurchaseOrderSelfPage = Repositorys.wmsPurchaseOrderSelf.findAll(
                WmsPurchaseOrderSelfSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return wmsPurchaseOrderSelfPage;
    }


    @Override
    public WmsPurchaseOrderSelf modify(WmsPurchaseOrderSelf wmsPurchaseOrderSelfNew) {
        return Repositorys.wmsPurchaseOrderSelf.saveAndFlush(WmsPurchaseOrderSelfFactory.initBaseFields(wmsPurchaseOrderSelfNew));
    }

    @Override
    public Optional<WmsPurchaseOrderSelf> single(UUID uuid) {
        return Repositorys.wmsPurchaseOrderSelf.findById(uuid);
    }

    @Override
    public List<WmsPurchaseOrderSelf> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.wmsPurchaseOrderSelf.findAllById(uuids);
    }

    @Override
    public List<WmsPurchaseOrderSelf> delete(List<WmsPurchaseOrderSelf> wmsPurchaseOrderSelfList) {
        wmsPurchaseOrderSelfList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            WmsPurchaseOrderSelfFactory.initBaseFields(item);
        });
        return Services.wmsPurchaseOrderSelfTransactional.batchModify(wmsPurchaseOrderSelfList);
    }

    @Override
    public WmsPurchaseOrderSelf getWmsOrderInfo(UUID uuid) {
        // 订单信息
        Optional<WmsPurchaseOrderSelf> wmsPurchaseOrderSelf =  this.single(uuid);
        // 查询订单行信息
        WmsPurchaseOrderLine wmsPurchaseOrderLine = new WmsPurchaseOrderLine();
        wmsPurchaseOrderLine.setWmsPurchaseOrderSelfUUID(uuid);
        Example<WmsPurchaseOrderLine> wmsPurchaseOrderLineEx = Example.of(wmsPurchaseOrderLine);
        List<WmsPurchaseOrderLine> wmsPurchaseOrderLines = Repositorys.wmsPurchaseOrderLine.findAll(wmsPurchaseOrderLineEx);
        WmsPurchaseOrderSelf purchaseOrderSelf =wmsPurchaseOrderSelf.get();
        purchaseOrderSelf.setWmsPurchaseOrderLines(wmsPurchaseOrderLines);
        return purchaseOrderSelf;
    }
}
